//
//  RZImageMessageCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/25.
//

#import "RZImageMessageCell.h"
#import "RZMessageCellTool.h"
#import <SDWebImage/SDWebImage.h>

@implementation RZImageMessageCell


#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    [self.messageContentView addSubview:self.pictureView];
}




#pragma mark - Super Methods

+ (CGSize)sizeForMessageModel:(RZMessageInfo *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    CGFloat messagecontentview_height = [self getMessageContentHeight:model];
    messagecontentview_height += extraHeight;
    return CGSizeMake(collectionViewWidth, messagecontentview_height);
    return CGSizeZero;
}

- (void)setDataModel:(RZMessageInfo *)model {
    [super setDataModel:model];
    [self setAutoLayout];
}


#pragma mark Private method
+ (CGFloat)getMessageContentHeight:(RZMessageInfo *)message{
    CGFloat messagecontentview_height = 0.0f;
    if (message.extraInfo && [message.extraInfo isKindOfClass:[RZImageMessageModel class]]) {
        RZImageMessageModel *model = (RZImageMessageModel *)message.extraInfo;
        CGSize imageSize = (CGSize){model.width,model.height};
        messagecontentview_height = [RZMessageCellTool getThumbnailImageSizeWithOrignalSize:imageSize].height;
    }
    
    if (messagecontentview_height < RZIMConfigCenter.ui.globalMessagePortraitSize.height) {
        messagecontentview_height = RZIMConfigCenter.ui.globalMessagePortraitSize.height;
    }
    return messagecontentview_height;
}


- (void)setAutoLayout {
    self.pictureView.image = nil;
    if (self.model.msg.length > 0) {
        RZImageMessageModel *extra = (RZImageMessageModel *)self.model.extraInfo;
        CGSize imageSize = (CGSize){extra.width,extra.height};
        
        CGSize thumbImageSize = [RZMessageCellTool getThumbnailImageSizeWithOrignalSize:imageSize];
        [self.pictureView sd_setImageWithURL:[NSURL URLWithString:self.model.msg] placeholderImage:extra.thumbImage options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            extra.thumbImage = image;
        }];
        
        self.messageContentView.contentSize = thumbImageSize;
        self.pictureView.frame = self.messageContentView.bounds;
    } else {
        NSLog(@"image is empty");
    }
}


#pragma mark - Getter
- (UIImageView *)pictureView{
    if (!_pictureView) {
        _pictureView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _pictureView.layer.masksToBounds = YES;
        _pictureView.layer.cornerRadius = 6;
        _pictureView.backgroundColor  = RGBCOLOR(220, 237, 251);
    }
    return _pictureView;
}

@end
